﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UcAsp.Net.Extract.Plug.Trends.BLL;
using UcAsp.Net.Extract.Plug.Trends.Model;
using UcAsp.Net.Interface;
using System.Text.RegularExpressions;
using System.Threading;
namespace UcAsp.Net.Extract.Plug.Trends
{
    public partial class UcAspControl : UserControl, IExtract
    {
        private delegate void DelegateUpdate(int i);
        private Thread t ;
        private DateTime _adddatetime = DateTime.Now;
        public UcAspControl()
        {
            InitializeComponent();
        }
        #region 属性
        /// <summary>
        /// 常规项
        /// </summary>
        /// 
       public  string Image { get; set; }
        public int ID { get; set; }
        public string Title { get; set; }
        public string Tag { get; set; }
        public string IconUrl { get; set; }
        public string ArticleContent { get; set; }
        public string MetaKeyword { get; set; }
        public string MetaDescrition { get; set; }
        public string Description { get; set; }
        public DateTime AddDatetime { get { return this._adddatetime; } set { this._adddatetime = value; } }
        public bool IsRelease { get; set; }
        public bool IsRecommend { get; set; }
        public int Hot { get; set; }
        public int Rate { get; set; }
        public int CategorieID { get; set; }
        public string ShowPath { get; set; }
        public string ViewsTemp { get; set; }
        public int UserID { get; set; }


        /// <summary>
        /// 可变文字项
        /// </summary>
        public string Note1 { get; set; }
        public string Note2 { get; set; }
        public string Note3 { get; set; }
        public string Note4 { get; set; }
        public string Note5 { get; set; }
        public string Note6 { get; set; }
        public string Note7 { get; set; }
        public string Note8 { get; set; }
        public string Note9 { get; set; }
        /// <summary>
        /// 可变数组项
        /// </summary>
        public int? Int1 { get; set; }
        public int? Int2 { get; set; }
        public int? Int3 { get; set; }
        public int? Int4 { get; set; }
        public int? Int5 { get; set; }


        public double Float1 { get; set; }
        public double Float2 { get; set; }
        public double Float3 { get; set; }
        public double Float4 { get; set; }
        public double Float5 { get; set; }


        public DateTime? Date1 { get; set; }
        public DateTime? Date2 { get; set; }
        public DateTime? Date3 { get; set; }
        public DateTime? Date4 { get; set; }
        public DateTime? Date5 { get; set; }
        public DateTime? Date6 { get; set; }
        public DateTime? Date7 { get; set; }
        public DateTime? Date8 { get; set; }
        public int PageCount { get; set; }
        #endregion
        public virtual bool Execute(object id)
        { return true; }
        public virtual object Init() { return new bool(); }
        public event EventHandler Save;
        public event EventHandler SavePage;
        public event EventHandler SaveImage;
        private void btnAuto_Click(object sender, EventArgs e)
        {
            //t = new Thread(new ThreadStart(UpdateData));
            //t.Name = "updatedata";
            //t.Start();
            UpdateData();
        }
        private void SmallContent(string url, Model.FilesConfig config)
        {
            RegexOptions options = RegexOptions.None;
            string pagecontent = HttpWebClient.GetString(url);
            //标题
            
            Regex pageregex = new Regex(config.TitleReg, options);
            Match match = pageregex.Match(pagecontent);
            string title = match.Groups["title"].Value;
            Title = title;
            //内容
            Regex contentregex = new Regex(config.ContentReg, options);
            Match contentmatch = contentregex.Match(pagecontent);
            string content = ClearCode(contentmatch.Groups["content"].Value,config.ClearReg);
           // richTextBox1.Text = richTextBox1.Text + "\r" + content;
            ArticleContent = content;
            
            if (SavePage != null)
            {
                SavePage(this, EventArgs.Empty);
            }
        }
       /// <summary>
       /// 清理内容中的多余代码
       /// </summary>
       /// <param name="cotent"></param>
       /// <param name="code"></param>
       /// <returns></returns>
        private string  ClearCode(string cotent, string code)
        {
            RegexOptions options = RegexOptions.None;
            Regex regex = new Regex(code, options);
            string input =cotent;
            string replacement = "";
            string result = regex.Replace(input, replacement);
            
            // TODO: Do something with result
            return result;
        }

        private void UcAspControl_Load(object sender, EventArgs e)
        {
            this.Width = this.Parent.Width;
            this.Height = this.Parent.Height;
        }
        private void UpdateUI(int i)
        {
            labNumber.Text = "已经更新"+i.ToString()+"条";
        }
        public void UpdateData()
        {
            List<Model.Config> list = BLL.Config.GetListConfig;
            Model.FilesConfig config = BLL.FilesConfig.GetFilesConfig(list[0].DefinedName);

            string urlcongent = HttpWebClient.GetString(config.Url);
            RegexOptions options = RegexOptions.None;
            Regex regex = new Regex(config.Reg, options);
            //Match match = regex.Match(urlcongent);
            MatchCollection matches = regex.Matches(urlcongent);
            //for (int j = 0; j < matches.Count; j++)
            //{
                string pageurl = matches[0].Groups["url"].Value;
                string pagecontent = HttpWebClient.GetString(config.RootPath + pageurl);

                AddDatetime = DateTime.Now; CategorieID = 27; IsRelease = true; Image = config.ImageReg;
                //标题
                Regex pageregex = new Regex(config.TitleReg, options);
                Match match = pageregex.Match(pagecontent);
                string title = match.Groups["title"].Value;
                Title = title;
                //内容
                Regex contentregex = new Regex(config.ContentReg, options);
                Match contentmatch = contentregex.Match(pagecontent);
                string content = contentmatch.Groups["content"].Value;
               // richTextBox1.Text = richTextBox1.Text + "\r" + ClearCode(content, config.ClearReg);
                //ArticleContent = ReplaceIMG(ClearCode(content, config.ClearReg),config.ImageReg);
                //分页页码
                Regex pagenumberregex = new Regex(config.PageReg, options);
                MatchCollection pagenumbermatch = pagenumberregex.Matches(pagecontent);
                if (Save != null)
                {
                    Save(this, EventArgs.Empty);
               }
                MessageBox.Show(this.ID.ToString());
                PageCount = pagenumbermatch.Count + 1;
                //this.Invoke(new DelegateUpdate(UpdateUI), j);
                if (PageCount > 1)
                {
                    for (int i = 0; i < pagenumbermatch.Count; i++)
                    {
                        //richTextBox1.Text = richTextBox1.Text + "\r" + config.RootPath + pagenumbermatch[i].Groups["url"].Value;

                        Int1 = i + 1;
                       //SmallContent(config.RootPath + pagenumbermatch[i].Groups["url"].Value, config);

                    }
                }
                ID = 0;

            //}
        }
    }
}
